package com.spboot.tx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jntoo.db.DB;
import com.spboot.tx.pojo.Dati;
import com.spboot.tx.pojo.Jieguo;
import com.spboot.tx.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 保存答题结果
 */

@Api(tags = { "评测提交" }, description = "评测提交")
@RestController
@RequestMapping("/api/paper")
public class PaperController{

    @PostMapping("/save")
    @ApiOperation(value = "提交数据信息", httpMethod = "POST")
    public ResponseData save( @RequestBody Map map )
    {
        JSONObject jsonObject = new JSONObject(map);
        JSONArray json = jsonObject.getJSONArray("list");
        HashMap data = new HashMap();
        String kaoshibianhao = Info.getID();

       // data.put("kaoshibianhao", kaoshibianhao);
        data.put("yonghu", Request.user().getId());


        HashMap m = new HashMap();
        for (int i = 0; i < json.size(); i++) {
            JSONObject obj = json.getJSONObject(i);
            obj.putAll(data);
            //obj.put("zhiyecepingid" , obj.get("id"));
            obj.remove("id");

            m.putAll(obj);
            DB.name("dati").insert(m);

        }



        HashMap jieguo = new HashMap();
        jieguo.putAll(m);

        //JSONObject leixingdefen = jsonObject.getJSONObject("leixingdefen");

        long total =jsonObject.getLongValue("zongdefen");

        Map daan = DB.name("jianyi").where("wenjuanid", jieguo.get("wenjuanid"))
                .where("defenxiaxian", "<", total).where("defenshangxian", ">=", total).find();
        if (daan == null) {
            jieguo.put("pinggu", "系统没有计算出您的当前情况噢");
        } else {
            jieguo.put("pinggu", daan.get("cankaoneirong"));
        }


        // jieguo.put("danxuantidefen", leixingdefen.containsKey("单选题") ? leixingdefen.getString("单选题") : 0);
        //jieguo.put("duoxuantidefen", leixingdefen.containsKey("多选题") ? leixingdefen.getString("多选题") : 0);
       // jieguo.put("panduantidefen", leixingdefen.containsKey("判断题") ? leixingdefen.getString("判断题") : 0);
       // jieguo.put("zongdefen", jsonObject.getString("zongdefen"));
        jieguo.put("zongdefen", total);
        jieguo.put("addtime", Info.getDateStr());
        //jieguo.put("kaoshishichang", Request.get("time"));
        int insertId = DB.name("jieguo").insert(jieguo);


        //DB.execute("UPDATE wxuser SET cepingfen='" + jieguo.get("zongdefen") + "' WHERE id='" + Request.user().getId() + "'");

        if(insertId > 0){
            return JsonResult.success(DB.name(Dati.class).find(insertId));
        }else{
            return JsonResult.error("添加错误");
        }
    }
}
